﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MinesweeperModel;

namespace Minesweeper
{
    
    public partial class Form1 : Form
    {
        private IMinesweeper ms = new TempMS{Columns = 5, Rows = 3};

        private Button[,] buttons;
        public Form1()
        {
            InitializeComponent();

            buttons = new Button[ms.Columns, ms.Rows];
            for (int x = 0; x < ms.Columns; x++ )
            {
                for (int y = 0; y < ms.Rows; y++)
                {
                    buttons[x,y]        = new Button();
                    buttons[x, y].Dock  = DockStyle.Fill;
                }
            }

            this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
            this.tableLayoutPanel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // tableLayoutPanel1
            // 
            this.tableLayoutPanel1.ColumnCount = ms.Columns;

            for (int i = 0; i < ms.Columns;i++ )
                this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F / ms.Columns));

            this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tableLayoutPanel1.Name = "tableLayoutPanel1";
            this.tableLayoutPanel1.RowCount = ms.Rows;

            for (int i = 0; i < ms.Rows; i++)
                this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F / ms.Rows));

            this.tableLayoutPanel1.Size = new System.Drawing.Size(497, 390);
            this.tableLayoutPanel1.TabIndex = 0;

            for (int x = 0; x < ms.Columns; x++)
            {
                for (int y = 0; y < ms.Rows; y++)
                {
                    tableLayoutPanel1.Controls.Add(buttons[x,y],x,y);
                }
            }


            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(497, 390);
            this.Controls.Add(this.tableLayoutPanel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.tableLayoutPanel1.ResumeLayout(false);
            this.ResumeLayout(false);


        }

        private void UpdateButtons()
        {
            Cell[,] cells = ms.Grid;

            for (int x = 0; x < ms.Columns; x++)
            {
                for (int y = 0; y < ms.Rows; y++)
                {
                    if (cells[x, y].isExposed)
                        buttons[x, y].Text = cells[x, y].NeighborBombCount.ToString();
                    else if (cells[x, y].IsFlagged)
                        buttons[x, y].Text = "F";
                }
            }
        }

        private Point GetLocation(Button b)
        {
            for (int x = 0; x < ms.Columns; x++)
            {
                for (int y = 0; y < ms.Rows; y++)
                {
                    if(buttons[x, y] == b )
                        return new Point(x,y);
                }
            }
            throw new Exception("Location not found...");
        }
    }

    internal class TempMS : IMinesweeper
    {
        public int Columns { get; set; }
        public int Rows { get; set; }
        public int BombCount { get; set; }
        public Cell[,] Grid { get; set; }

        public void Init()
        {
            throw new NotImplementedException();
        }

        public void Flag(int r, int c)
        {
            throw new NotImplementedException();
        }

        public void Expose(int r, int c)
        {
            throw new NotImplementedException();
        }

        public int BombsRemaining()
        {
            throw new NotImplementedException();
        }
    }
}
